package com.white.controller;

import com.white.bean.Communicate;
import com.white.bean.Follow;
import com.white.bean.FollowView;
import com.white.bean.User;
import com.white.service.CommunicateService;
import com.white.service.FollowService;
import com.white.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FollowController {

    @Autowired
    FollowService followService;

    @Autowired
    UserService userService;

    @Autowired
    CommunicateService communicateService;
    /**
     * 关注与取关
     * @param followUserId
     * @param beFollowUserId
     * @return
     */
    @GetMapping("/follow/{beFollowUserId}")
    @ResponseBody
    public void follow(@CookieValue(UserController.LOGIN_USER_ID) Long followUserId,
                       @PathVariable("beFollowUserId") Long beFollowUserId) {
        Follow follow = new Follow(beFollowUserId, followUserId);
        followService.save(follow);
    }

    @GetMapping("/unFollow/{beFollowUserId}")
    @ResponseBody
    public void unFollow(@CookieValue(UserController.LOGIN_USER_ID) Long followUserId,
                             @PathVariable("beFollowUserId") Long beFollowUserId) {
        Follow follow = new Follow(beFollowUserId, followUserId);
        followService.delete(follow);
    }


    @GetMapping("/isFollow/{beFollowUserId}")
    @ResponseBody
    public boolean isFollow(@CookieValue(UserController.LOGIN_USER_ID) Long followUserId,
                            @PathVariable("beFollowUserId") Long beFollowUserId){
        Follow follow = new Follow(beFollowUserId, followUserId);
        List<Follow> follows = followService.mySearch(follow);
        return follows.size() != 0;
    }

    /**
     * 根据用户ID获取关注用户列表
     * @return
     */
    @GetMapping("/getFollow")
    @ResponseBody
    public List<User> getFollow(@CookieValue(UserController.LOGIN_USER_ID) Long loginUserId) {
        return userService.getFollowByUserId(loginUserId);
    }


    /**
     * 根据用户ID获取关注用户列表
     * @return
     */
    @GetMapping("/getFan")
    @ResponseBody
    public List<User> getFan(@CookieValue(UserController.LOGIN_USER_ID) Long loginUserId) {
        return userService.getFanByUserId(loginUserId);
    }

    @GetMapping("/follow")
    public String follow() {
        return "follow";
    }

    @GetMapping("/follow.html")
    public String redirectFollow() {
        return "follow";
    }



    @GetMapping("/getFollowViewById/{userId}")
    @ResponseBody
    public List<FollowView> getFollowViewById(@PathVariable("userId") Long userId) {
        return followService.getFollowById(userId);
    }


    @GetMapping("/getFollowView")
    @ResponseBody
    public List<FollowView> getFollowView(@CookieValue(UserController.LOGIN_USER_ID) Long userId) {
        return followService.getFollowById(userId);
    }

    @GetMapping("/getCommunicateFollowView")
    @ResponseBody
    public List<Object> getCommunicateFollowView(@CookieValue(UserController.LOGIN_USER_ID) Long userId) {
        /*获取关注列表*/
        List<User> users = userService.getFollowByUserId(userId);
        return getCommunicateAndUser(userId, users);
    }

    /**
     * 获取最新的交流数据
     * @param userId
     * @return
     */
    @GetMapping("/getLatestCommunicateFollowView")
    @ResponseBody
    public List<Object> getLatestCommunicateFollowView(@CookieValue(UserController.LOGIN_USER_ID) Long userId) {
        /*获取粉丝列表*/
        List<User> users = userService.getFollowByUserId(userId);
        return getLatestCommunicateAndUser(userId, users);
    }

    @GetMapping("/getFanViewById/{userId}")
    @ResponseBody
    public List<FollowView> getFanViewById(@PathVariable("userId") Long userId) {
        return followService.getFanById(userId);
    }

    @GetMapping("/getFanView")
    @ResponseBody
    public List<FollowView> getFanView(@CookieValue(UserController.LOGIN_USER_ID) Long userId) {
        return followService.getFanById(userId);
    }

    @GetMapping("/getCommunicateFanView")
    @ResponseBody
    public List<Object> getCommunicateFanView(@CookieValue(UserController.LOGIN_USER_ID) Long userId) {
        /*获取粉丝列表*/
        List<User> users = userService.getFanByUserId(userId);
        return getCommunicateAndUser(userId, users);
    }



    @GetMapping("/getLatestCommunicateFanView")
    @ResponseBody
    public List<Object> getLatestCommunicateFanView(@CookieValue(UserController.LOGIN_USER_ID) Long userId) {
        /*获取粉丝列表*/
        List<User> users = userService.getFanByUserId(userId);
        return getLatestCommunicateAndUser(userId, users);
    }


    /**
     * 通过用户列表获取交流和用户数据
     * @param userId
     * @param list
     * @return
     */
    public List<Object> getCommunicateAndUser(Long userId, List<User> list) {
        List<Object> ans = new ArrayList<>();
        for (User user : list) {
            Map<String, Object> map = new HashMap<>();
            List<Communicate> communicates = communicateService.search(userId, user.getUserId());
            map.put("follow", user);
            map.put("communicates", communicates);
            ans.add(map);
        }
        return ans;
    }

    public List<Object> getLatestCommunicateAndUser(Long userId, List<User> list) {
        List<Object> ans = new ArrayList<>();
        for (User user : list) {
            Map<String, Object> map = new HashMap<>();
            List<Communicate> communicates = communicateService.searchLatest(userId, user.getUserId());
            map.put("follow", user);
            map.put("communicates", communicates);
            ans.add(map);
        }
        return ans;
    }



}
